﻿<UserControl x:Class="WPF3DUserControl.CubeUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <UserControl.Resources>
        <DiffuseMaterial x:Key="material"     Brush="CornflowerBlue" />
        <DiffuseMaterial x:Key="backMaterial" Brush="Red" />
        <GeometryModel3D x:Key="side"
                         Material="{StaticResource material}"
                         BackMaterial="{StaticResource backMaterial}">
            <GeometryModel3D.Geometry>
                <MeshGeometry3D Positions="1 0 0, 1 1 0, 0 0 0, 0 1 0"
                                TriangleIndices="0 1 2, 1 3 2" />
            </GeometryModel3D.Geometry>
        </GeometryModel3D>
    </UserControl.Resources>
    
    <UserControl.Triggers>
        <EventTrigger RoutedEvent="Page.Loaded">
            <BeginStoryboard>
                <Storyboard RepeatBehavior="Forever" SpeedRatio="1.5">
                    <DoubleAnimation Storyboard.TargetName="model"
                                     Storyboard.TargetProperty="Angle"
                                     From="0" To="360" Duration="0:0:20" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </UserControl.Triggers>
    
    <Viewport3D>

        <!-- Main Model -->
        <ModelVisual3D>

            <!-- Front -->
            <ModelVisual3D Content="{StaticResource side}" />

            <!-- Left -->
            <ModelVisual3D Content="{StaticResource side}">
                <ModelVisual3D.Transform>
                    <Transform3DGroup>
                        <TranslateTransform3D OffsetX="-1" />
                        <RotateTransform3D>
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D Angle="90" Axis="0 -1 0" />
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </Transform3DGroup>
                </ModelVisual3D.Transform>
            </ModelVisual3D>

            <!-- Back -->
            <ModelVisual3D Content="{StaticResource side}">
                <ModelVisual3D.Transform>
                    <Transform3DGroup>
                        <TranslateTransform3D OffsetX="-1" OffsetZ="1" />
                        <RotateTransform3D>
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D Angle="180" Axis="0 -1 0" />
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </Transform3DGroup>
                </ModelVisual3D.Transform>
            </ModelVisual3D>

            <!-- Right -->
            <ModelVisual3D Content="{StaticResource side}">
                <ModelVisual3D.Transform>
                    <Transform3DGroup>
                        <TranslateTransform3D OffsetZ="1" />
                        <RotateTransform3D>
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D Angle="270" Axis="0 -1 0" />
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                    </Transform3DGroup>
                </ModelVisual3D.Transform>
            </ModelVisual3D>

            <ModelVisual3D.Transform>
                <RotateTransform3D CenterY=".5" CenterZ="-.5">
                    <RotateTransform3D.Rotation>
                        <AxisAngleRotation3D x:Name="model" Angle="0" Axis="1 0 0" />
                    </RotateTransform3D.Rotation>
                </RotateTransform3D>
            </ModelVisual3D.Transform>

        </ModelVisual3D>

        <!-- Lights -->
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <AmbientLight Color="#404040" />
                    <DirectionalLight Color="#C0C0C0" Direction="2 -3 -1" />
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>

        <!-- Camera -->
        <Viewport3D.Camera>
            <PerspectiveCamera x:Name="cam"
                Position="-1 1.75 2.5" 
                LookDirection="0.5 -0.5 -1"
                FieldOfView="45" />
        </Viewport3D.Camera>
    </Viewport3D>

</UserControl>